﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using PPIDesktopClient.Entities;

namespace PPIDesktopClient.General
{
    public class XMLConfigManager
    {
        private const string K_CONFIGFILENAME = "C:\\PolePosition\\PolepositionConfig.xml";
        private const string K_SETTINGSNODE = "Settings";
        private const string K_SETTINGNODE = "Setting";
        private const string K_KEYATTRIBUTE = "Key";
        private const string K_VALUEATTRIBUTE = "Value";

        public XMLConfigManager()
        {
            if (!File.Exists(K_CONFIGFILENAME))
            {
                CreateConfigFile();
                ConfigFile();
            }
        
        }

        private void CreateConfigFile()
        {
            XmlDocument _xmlDocument = new XmlDocument();
            _xmlDocument.AppendChild(_xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null));
            _xmlDocument.AppendChild(_xmlDocument.CreateElement(K_SETTINGSNODE));
            _xmlDocument.Save(K_CONFIGFILENAME);
        }

        private void ConfigFile()
        {
            XmlDocument _xmlDocument = new XmlDocument();
            _xmlDocument.Load(K_CONFIGFILENAME);
            XmlNode _settingsNode = _xmlDocument.ChildNodes[1]; //Settings Node

            XmlNode _settingNode = _xmlDocument.CreateElement(K_SETTINGNODE);
            XmlAttribute _settingAttribute = _xmlDocument.CreateAttribute(K_KEYATTRIBUTE);
            _settingAttribute.Value = "APPLICATION";
            _settingNode.Attributes.Append(_settingAttribute);

            _settingAttribute = _xmlDocument.CreateAttribute(K_VALUEATTRIBUTE);
            _settingAttribute.Value = "BETA";
            _settingNode.Attributes.Append(_settingAttribute);

            _settingsNode.AppendChild(_settingNode);


            _settingNode = _xmlDocument.CreateElement(K_SETTINGNODE);
            _settingAttribute = _xmlDocument.CreateAttribute(K_KEYATTRIBUTE);
            _settingAttribute.Value = "MobileVersion";
            _settingNode.Attributes.Append(_settingAttribute);

            _settingAttribute = _xmlDocument.CreateAttribute(K_VALUEATTRIBUTE);
            _settingAttribute.Value = "";
            _settingNode.Attributes.Append(_settingAttribute);

            _settingsNode.AppendChild(_settingNode);

            _xmlDocument.Save(K_CONFIGFILENAME);

        }

        public EBaseSettings ReadBySetting(EBaseSettings settingToRead)
        {
            EBaseSettings _returnSettings = (EBaseSettings)settingToRead.Clone();
            XmlNode _ReadSetting = GetNodeByKey(settingToRead.Key);
            if (_ReadSetting != null)
            {
                _returnSettings.Value = _ReadSetting.Attributes[K_VALUEATTRIBUTE].Value;
            }
            return _returnSettings;
        }

        private XmlNode GetNodeByKey(string key)
        {
            XmlDocument _xmlDocument = new XmlDocument();
            _xmlDocument.Load(K_CONFIGFILENAME);
            XmlNode _ReadSetting = _xmlDocument.SelectSingleNode(@"" + K_SETTINGSNODE + "/" + K_SETTINGNODE + "[@" + K_KEYATTRIBUTE + "='" + key + "']");
            return _ReadSetting;
        }

        public void UpdateValue(EBaseSettings settingsToUpdate)
        {
            XmlDocument _xmlDocument = new XmlDocument();
            _xmlDocument.Load(K_CONFIGFILENAME);
            XmlNode _NodeToUpdate = _xmlDocument.SelectSingleNode(@"" + K_SETTINGSNODE + "/" + K_SETTINGNODE + "[@" + K_KEYATTRIBUTE + "='" + settingsToUpdate.Key + "']");
            _NodeToUpdate.Attributes[K_VALUEATTRIBUTE].Value = settingsToUpdate.Value;
            _xmlDocument.Save(K_CONFIGFILENAME);
        }


    }
}
